<?php
class PurchaseForm extends BaseForm {
  const FORM_NAME                 = 'frm';

//  const FIELD_NUMBER_OF_TICKETS   = 'number_of_tickets';
//  const FIELD_FIRST_NAME          = 'first_name';
//  const FIELD_LAST_NAME           = 'last_name';
  const FIELD_EMAIL               = 'email';
//  const FIELD_PHONE               = 'phone';
//  const FIELD_ADDRESS             = 'address1';
//  const FIELD_ADDRESS2            = 'address2';
//  const FIELD_CITY                = 'city';
//  const FIELD_STATE_ID            = 'state_id';
//  const FIELD_POST_CODE           = 'post_code';
  const FIELD_AGREE_TC            = 'agree_tc';
  const FIELD_BECOME_MEMBER       = 'become_member';
  const FIELD_CARD_NAME           = 'card_name';
  const FIELD_CARD_NUMBER         = 'card_number';
  const FIELD_CARD_DATE           = 'card_date';
  const FIELD_CARD_CVN            = 'card_cvn';

  private $seatsAvailableCount    = null;

  public function  __construct($seatsAvailableCount) {
    $this->seatsAvailableCount = $seatsAvailableCount;
    parent::__construct();
  }

  public function  configure() {
    $arrStates = STATETable::getInstance()->findAllArray(true);

    $arrYears = array();
    for ($i=(date('Y')+5); $i>=date('Y'); $i--) {
      $arrYears[$i] = $i;
    }

    $arrMonths = array();
    for ($i=1; $i<=12; $i++) {
      $arrMonths[$i] = date('M', mktime(1,1,1,$i));
    }

    //$maxQuantity = ($this->seatsAvailableCount > 10) ? 10 : $this->seatsAvailableCount;

    $widget = array(
//            self::FIELD_NUMBER_OF_TICKETS => new sfWidgetFormSelect(array('choices' => array_combine(range(1, $maxQuantity), range(1, $maxQuantity))), array('multiple' => false, 'expand' => true)),
//            self::FIELD_FIRST_NAME        => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
//            self::FIELD_LAST_NAME         => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_EMAIL             => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
//            self::FIELD_PHONE             => new sfWidgetFormInputText(array(), array('maxlength' => 30)),
//            self::FIELD_ADDRESS           => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
//            self::FIELD_ADDRESS2          => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
//            self::FIELD_CITY              => new sfWidgetFormInputText(array(), array('maxlength' => 100)),
//            self::FIELD_STATE_ID          => new sfWidgetFormSelect(array('choices' => $arrStates), array('multiple' => false, 'expand' => true)),
//            self::FIELD_POST_CODE         => new sfWidgetFormInputText(array(), array('maxlength' => 20)),
            self::FIELD_CARD_NAME         => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_CARD_NUMBER       => new sfWidgetFormInputText(array(), array('maxlength' => 16)),
            self::FIELD_CARD_DATE         => new sfWidgetFormDate(array('format' => '%month% %year%', 'years' => $arrYears, 'months' => $arrMonths)),
            self::FIELD_CARD_CVN          => new sfWidgetFormInputText(array(), array('maxlength' => 3)),
            self::FIELD_AGREE_TC          => new sfWidgetFormInputCheckbox(),
            self::FIELD_BECOME_MEMBER     => new sfWidgetFormInputCheckbox()
    );

    $labels = array(
//            self::FIELD_NUMBER_OF_TICKETS => 'Quantity',
//            self::FIELD_FIRST_NAME        => 'First Name',
//            self::FIELD_LAST_NAME         => 'Last Name',
            self::FIELD_EMAIL             => 'Email Address',
//            self::FIELD_PHONE             => 'Phone',
//            self::FIELD_ADDRESS           => 'Address',
//            self::FIELD_ADDRESS2          => 'Address 2',
//            self::FIELD_CITY              => 'City',
//            self::FIELD_STATE_ID          => 'State',
//            self::FIELD_POST_CODE         => 'Post Code',
            self::FIELD_AGREE_TC          => 'Agree to terms and conditions',
            self::FIELD_BECOME_MEMBER     => 'Become a member',
            self::FIELD_CARD_NAME         => 'Card Holder Name',
            self::FIELD_CARD_NUMBER       => 'Card Number',
            self::FIELD_CARD_DATE         => 'Expiry Date',
            self::FIELD_CARD_CVN          => 'CVN'
    );

    $this->setWidgets($widget);
    $this->widgetSchema->setLabels($labels);

    $validators = array(
//            self::FIELD_NUMBER_OF_TICKETS => new sfValidatorNumber(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_NUMBER_OF_TICKETS])))),
//            self::FIELD_FIRST_NAME        => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_FIRST_NAME])))),
//            self::FIELD_LAST_NAME         => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_LAST_NAME])))),
            self::FIELD_EMAIL             => new sfValidatorEmail(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_EMAIL])), 'invalid' => LangUtil::getText('error_invalid', array($labels[self::FIELD_EMAIL])))),
//            self::FIELD_ADDRESS           => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_ADDRESS])))),
//            self::FIELD_CITY              => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_CITY])))),
//            self::FIELD_STATE_ID          => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_STATE_ID])))),
//            self::FIELD_POST_CODE         => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_POST_CODE])))),
            self::FIELD_CARD_NAME         => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_CARD_NAME])))),
            self::FIELD_CARD_NUMBER       => new sfValidatorString(array('trim' => true, 'min_length' => 13), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_CARD_NUMBER])), 'min_length' => LangUtil::getText('error_min_length', array($labels[self::FIELD_CARD_NUMBER], 13)))),
            self::FIELD_CARD_CVN          => new sfValidatorString(array('trim' => true, 'min_length' => 3), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_CARD_CVN])), 'min_length' => LangUtil::getText('error_min_length', array($labels[self::FIELD_CARD_CVN], 3)))),
            self::FIELD_AGREE_TC          => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_not_accepted')))
    );

    $this->setValidators($validators);
    $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidator'))));

    $this->widgetSchema->setNameFormat(self::FORM_NAME . '[%s]');

    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
  }

  public function postValidator($validator, $values) {
    if (!$values[self::FIELD_CARD_DATE]['month'] || !$values[self::FIELD_CARD_DATE]['year']) {
      $error = new sfValidatorError($validator, LangUtil::getText('error_required', array('Expiry Date')));
      throw new sfValidatorErrorSchema($validator, array(self::FIELD_CARD_DATE => $error));
    }
    
    /*if ($this->seatsAvailableCount && $values[self::FIELD_NUMBER_OF_TICKETS]) {
      if ($values[self::FIELD_NUMBER_OF_TICKETS] > $this->seatsAvailableCount) {
        $error = new sfValidatorError($validator, LangUtil::getText('error_tickets_unavailable'));
        throw new sfValidatorErrorSchema($validator, array(self::FIELD_NUMBER_OF_TICKETS => $error));
      }
    }*/

    return $values;
  }

  public function populateData(TICKET $ticket) {
//    $this->setDefault(self::FIELD_NUMBER_OF_TICKETS, $ticket->getQuantity());
//    $this->setDefault(self::FIELD_FIRST_NAME, $ticket->getFirstName());
//    $this->setDefault(self::FIELD_LAST_NAME, $ticket->getLastName());
    $this->setDefault(self::FIELD_EMAIL, $ticket->getEmail());
//    $this->setDefault(self::FIELD_PHONE, $ticket->getPhone());
//    $this->setDefault(self::FIELD_ADDRESS, $ticket->getAddress());
//    $this->setDefault(self::FIELD_ADDRESS2, $ticket->getAddress2());
//    $this->setDefault(self::FIELD_CITY, $ticket->getCity());
//    $this->setDefault(self::FIELD_STATE_ID, $ticket->getStateId());
//    $this->setDefault(self::FIELD_POST_CODE, $ticket->getPostCode());
    
  }
}
?>